{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "---\n",
    "title: LCD显示图片转码工具（image2lcd online）  ️\n",
    "description: 二值化图像,并用创建c数组供LCD显示.Make images Binaryized and the create c language array from uploaded a photo.\n",
    "show-code: False\n",
    "params:\n",
    "    image_path:\n",
    "        input: file\n",
    "        label: 请上传图片.Please upload image\n",
    "    filename:\n",
    "        input: text\n",
    "        label: 输出文件名.output filename\n",
    "        value: HEX.txt\n",
    "    xpixels:\n",
    "        input: slider\n",
    "        label: 输出横向像素个数. number of output x-axis pixels\n",
    "        value: 120\n",
    "        min: 8\n",
    "        max: 200\n",
    "    ypixels:\n",
    "        input: slider\n",
    "        label: 输出纵向像素个数. number of output y-axis pixels\n",
    "        value: 120\n",
    "        min: 8\n",
    "        max: 200\n",
    "    threshold:\n",
    "        input: slider\n",
    "        label: 二值化阈值. Binaryized threshold\n",
    "        value: 122\n",
    "        min: 0\n",
    "        max: 255\n",
    "    isOX:\n",
    "        label: 是否输出16进制标识符 (OX)?whether output \"OX\"?\n",
    "        input: checkbox\n",
    "        value: False\n",
    "    Horizontal_scan:\n",
    "        label: 是否横向扫描?whether scan horizontally?\n",
    "        input: checkbox\n",
    "        value: True\n",
    "    output_dir:\n",
    "        output: dir\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import cv2\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_path = \"Screenshot from 2022-03-04 15-31-04.png\"\n",
    "filename = \"HEX.txt\"\n",
    "xpixels =120\n",
    "ypixels =120\n",
    "isOX = False\n",
    "Horizontal_scan = True\n",
    "output_dir = \"example_output_directory\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,20,00,00,00,00,00,00,00,00,00,00,00,00,00,20,02,00,00,00,00,00,00,00,00,00,00,00,00,00,20,00,00,00,00,00,00,00,00,00,00,00,00,00,00,18,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,20,00,00,00,00,00,00,00,00,00,00,00,00,00,3C,03,00,00,00,00,00,00,00,00,00,00,00,00,00,3E,00,00,00,00,00,00,00,00,00,00,00,00,00,00,3E,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,40,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,08,00,00,00,00,00,00,00,00,00,00,00,00,00,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,02,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,03,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,18,40,00,00,00,00,1C,61,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,20,00,00,82,00,20,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,01,00,04,00,00,00,00,00,2E,20,00,00,00,00,10,00,00,00,00,00,00,00,00,1E,00,00,10,00,00,10,00,00,10,00,00,00,00,00,3E,00,00,00,00,80,30,00,04,08,60,00,00,00,00,00,00,00,20,43,80,10,00,04,00,60,00,00,00,00,08,00,00,10,03,00,00,00,00,08,00,00,00,00,00,3C,01,00,00,03,06,00,01,0C,10,00,00,00,00,00,3C,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,03,80,00,01,00,00,00,00,00,00,00,00,01,80,00,03,00,00,02,00,00,00,00,00,00,00,00,00,00,00,03,80,00,00,84,08,60,00,00,00,00,00,00,00,00,00,00,00,00,04,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,18,10,00,00,00,00,00,00,00,00,00,00,00,00,0C,10,40,00,02,00,00,00,03,00,00,00,00,00,00,00,00,40,10,00,00,00,00,00,00,00,00,00,00,02,80,08,40,00,00,00,00,00,00,00,00,00,00,00,02,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,10,03,00,00,00,00,00,00,00,00,00,01,00,00,60,10,03,00,00,00,00,00,00,00,00,00,02,80,00,00,10,03,00,00,00,00,00,00,00,00,00,02,00,18,40,10,00,00,00,00,00,00,00,00,00,00,00,00,08,40,00,00,00,00,00,00,00,00,00,00,00,00,00,00,10,00,00,00,00,00,00,00,00,00,00,00,01,80,00,00,10,00,00,00,00,00,00,00,00,00,00,03,80,00,00,00,00,00,00,00,00,00,00,00,00,00,02,80,18,00,10,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,04,00,00,00,00,00,00,00,00,00,00,00,00,00,00,18,10,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,02,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,08,00,00,00,00,00,00,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,01,00,00,00,00,00,01,00,08,00,00,00,00,00,00,21,80,00,00,00,00,00,00,08,20,00,00,00,00,00,50,00,00,00,00,00,00,00,08,00,00,00,00,00,00,20,00,00,00,00,00,03,00,00,00,00,00,00,00,00,61,00,00,00,00,00,00,00,00,00,00,00,00,00,00,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,01,00,00,20,00,00,00,00,00,01,00,00,00,00,00,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,08,20,00,00,00,00,00,00,00,00,00,00,00,00,00,04,10,00,00,00,00,00,00,00,00,00,00,00,00,80,00,00,00,00,00,00,00,00,00,00,00,00,00,02,00,08,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,10,00,00,00,00,00,00,00,00,00,00,00,00,02,00,00,00,00,00,00,00,00,00,00,00,00,00,00,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,08,00,00,00,00,00,00,01,00,00,00,00,00,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,03,00,00,60,00,00,00,00,00,02,00,00,00,00,00,01,00,00,00,00,00,00,00,00,01,00,00,00,00,00,00,00,00,20,00,00,00,00,00,00,00,00,00,00,00,00,00,08,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,01,00,00,00,00,00,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,03,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,02,08,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,0C,00,00,00,00,00,00,00,00,00,00,00,00,00,01,08,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,04,00,00,00,00,00,00,00,00,00,00,00,00,00,00,04,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,04,00,00,00,00,00,00,00,00,00,00,00,00,00,00,04,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,08,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,70,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,04,00,00,00,00,00,00,00,00,00,00,00,00,00,00,04,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,20,00,00,00,00,00,00,00,00,00,00,00,00,00,00,20,0E,00,00,00,00,00,00,00,00,00,00,00,00,00,00,0E,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,03,00,00,00,00,00,00,00,00,00,00,00,00,00,00,40,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,21,00,00,00,00,00,00,00,00,00,00,00,00,00,0C,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\n"
     ]
    }
   ],
   "source": [
    "img = cv2.imread(image_path,cv2.IMREAD_GRAYSCALE)\n",
    "img = cv2.resize(img, (ypixels, xpixels))\n",
    "res = np.zeros(shape=(ypixels, xpixels))\n",
    "lens = img.shape[0]*img.shape[1]\n",
    "for i in range(img.shape[0]):\n",
    "    for j in range(img.shape[1]):\n",
    "        if img[i,j] > threshold:\n",
    "            tmp = 1\n",
    "        else:\n",
    "            tmp = 0\n",
    "        if Horizontal_scan:\n",
    "            res[j,i] = round(tmp,1)\n",
    "        else:\n",
    "            res[i,j] = round(tmp,1)\n",
    "res = res.reshape(-1)\n",
    "i=0\n",
    "itr =0\n",
    "encoder_img = ''\n",
    "while i < lens:\n",
    "    tmp_str = ''\n",
    "    for j in range(8):\n",
    "        tmp_str += str(res[i+j])[:1]\n",
    "    hex_str = hex(int(tmp_str,2))\n",
    "    hex_str = hex(int(tmp_str,2)).swapcase()\n",
    "    if len(hex_str)<4:\n",
    "        hex_str=hex_str[:2]+'0'+hex_str[-1]\n",
    "    if isOX:\n",
    "        encoder_img += hex_str\n",
    "    else:\n",
    "        encoder_img += hex_str[-2:]\n",
    "    encoder_img += ','\n",
    "    i += 8\n",
    "    itr+=1\n",
    "\n",
    "print(encoder_img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(os.path.join(output_dir, filename), \"w\") as fout:\n",
    "    fout.write(encoder_img)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
